package com.sky.controller.admin;

import com.sky.constant.ShopConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

/**
 * 店铺管理
 */
@RestController
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {
    private RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();

    /**
     * 获取店铺营业状态
     *
     * @return
     */
//    @GetMapping("/status")
    public Result<Integer> getStatus() {
        ValueOperations<String, Integer> opsForValue = redisTemplate.opsForValue();
        Integer status = opsForValue.get(ShopConstant.SHOP_STATUS_REDIS_KEY);

        return Result.success(status);
    }

    /**
     * 设置店铺营业状态
     *
     * @return
     */
//    @PutMapping("/{status}")
    @CacheEvict(value = ShopConstant.SHOP_STATUS_REDIS_KEY, allEntries = true)
    public Result<String> setStatus(@PathVariable Integer status) {
        ValueOperations<String, Integer> opsForValue = redisTemplate.opsForValue();
        opsForValue.set(ShopConstant.SHOP_STATUS_REDIS_KEY, status);

        return Result.success();
    }
}